<%@ page isELIgnored="false" language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>


<script type="text/javascript">
    $(function(){
        $("#addArticleDialog").dialog({
            title: "添加框",
            width: 600,
            height: 300,
            closed: true,
            buttons:[{
                text:"添加",
                handler:function(){
                    doadd();
                },
                text:"添加",
                handler:function(){
                    doadd();
                }
            }],
        });
        $("#updateArticleDialog").dialog({
            title: "修改框",
            width: 600,
            height: 500,
            closed: true,
            buttons:[{
                text:"修改",
                handler:function(){
                    doUpdateArticle();
                },
            }],
        });



       $("#allArticle").datagrid({
           fit:"true",
           pagination:"true",
           url:"${pageContext.request.contextPath}/Article/selectAllArticle.do",
           columns:[[
               {field:'albumId',checkbox:"true"},
               {field:'albumImage',title:'文章图片',width:100,formatter: function(value,row,index){
                   return "<img style='width:32px;height:32px;' border='1' src='${pageContext.request.contextPath}/img"+row.articleImage+"'/>";
                   }
               },

               {field:'articleName',title:'文章名',width:100},
               {field:'articleCount',title:'文章内容',width:100},

               {field:'gurnName',title:'上师姓名',width:100},
               {field:'articleDate',title:'发表日期',width:100},
               {field:'articleState',title:'是否显示',width:100,
                    formatter: function(value,row,index){
                        if (row.articleState==0){
                            console.log(row.articleState);
                            return "未冻结";
                        } else {
                            console.log(row.articleState);
                            return "已冻结2";
                        }
                    }
                 },
           ]],
           toolbar: [{
               /*添加工具  */
               text: "添加",
               iconCls: 'icon-add',
               handler: function () {
                   /*打开添加对话框  */
                   $("#addArticleDialog").panel('open');
                   $.ajax({
                       url: "${pageContext.request.contextPath}/Guru/selectAllGuru2.do",
                       type: "post",
                       /*设置同步请求  */
                       async:false,
                       //提示ajax不做深度解析
                       traditional:true,
                       success: function (data) {
                           data2=data.rows;
                           console.log(data2);
                           for(var i=0,l=data2.length;i<l;i++){
                               var guruId = data2[i].guruId;
                               var guruName = data2[i].guruName;
                               var sel = document.getElementById("guruId");//获取下拉选对象
                               var option = new Option(guruName,guruId);
                               sel.options.add(option);
                           }
                       }
                   });
               },
               /*添加工具-----------end  */
           },{
               iconCls: 'icon-help',
               text:'删除选中',
               handler: function() {
                   $.messager.confirm("确认框", "您确认想要删除记录吗？", function (r) {
                       if (r) {
                           /*获取选中的行对象  */
                           var hang = $("#allArticle").datagrid("getSelections");
                           var ids = new Array();
                           for (var i = 0; i < hang.length; i++) {
                               ids[i] = hang[i].articleId;
                           }
                           /*重新发送ajax请求删除选中的id集合  */
                           $.ajax({
                               url: "${pageContext.request.contextPath}/Article/delateArticle.do",
                               data: {"ids": ids},
                               type: "post",
                               /*提示ajax不做深度解析  */
                               traditional: true,
                               success: function (data) {
                                   if (data.isok) {
                                       $.messager.alert("消息", "删除成功");
                                       $("#allArticle").datagrid("reload");
                                   } else {
                                       $.messager.alert("消息", "删除失败");
                                       $("#allArticle").datagrid("reload");
                                   }
                               }
                           });
                       }
                   });
               }
           },{
               iconCls: 'icon-help',
               text:"重置索引库",
               handler:function(){
                   $.ajax({
                       url: "${pageContext.request.contextPath}/LuceneController/resetIndexDB.do",
                       type: "post",
                       /*设置同步请求  */
                       async:false,
                       //提示ajax不做深度解析
                       traditional:true,
                       success: function (data) {
                           if (data.isok) {
                               $.messager.alert("消息", "重置成功");
                               document.getElementById("sss").value="";
                               var key='';
                               $('#allArticle').datagrid('load',{
                                   atricleName:key,
                               });
                               $("#allArticle").datagrid("reload");
                           } else {
                               $.messager.alert("消息", "重置失败");
                               document.getElementById("sss").value="";
                               var key='';
                               $('#allArticle').datagrid('load',{
                                   atricleName:key,
                               });
                               $("#allArticle").datagrid("reload");
                           }
                       }
                   });
               },
           }],
           onDblClickRow: function (index, rowDate) {
              $("#updateArticleDialog").panel('open');
              $("#articleId").val(rowDate.articleId);
              $("#articleImage").val(rowDate.articleImage);
               $("#articleInageEcho2").prop("src","${pageContext.request.contextPath}/img"+rowDate.articleImage);
              $("#articleName").val(rowDate.articleName);
               $("#articleCount").val(rowDate.articleCount);
                 if(rowDate.articleState==0){
                    $("#radio1").prop("checked",true);
                }else {
                    $("#radio2").prop("checked",true);
                }
               console.log(rowDate);
           },
       });
    });



    function doadd(){
        $("#addform").form("submit",{
            url:"${pageContext.request.contextPath}/Article/addArticle.do",
            onSubmit: function(){
                return true;
            },
            traditional: true,
            success:function(data){
                var data=JSON.parse(data);
                console.log(data+"******");
                if(data.isok){
                    $.messager.alert("消息","添加成功");
                    $("#addArticleDialog").dialog('close');
                    $("#allArticle").datagrid("reload");
                }else{
                    $.messager.alert("消息","添加失败");
                };
            }
        });
    }

    function doUpdateArticle(){
        $("#upArticledateform").form("submit",{
            url:"${pageContext.request.contextPath}/Article/updateArticle.do",
            onSubmit: function(){
                return true;
            },
            success:function(data){
                var data = eval('(' + data + ')');  // change the JSON string to javascript object
                if(data.isok){
                    $.messager.alert("消息","修改成功");
                    /*刷新table  */
                    $("#updateArticleDialog").dialog("close",true);
                    $("#allArticle").datagrid("reload");
                }else{
                    $.messager.alert("消息","修改失败");
                };
            },

        });
    }


    function doSelectLucene(){
        var key=$("#sss").val();
        $('#allArticle').datagrid('load',{
            atricleName:key,
        });


    }




</script>


<script type="text/javascript">
    var image = '';
    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById('articleInageEcho').src = evt.target.result;
            image = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
</script>
<script type="text/javascript">
    var image = '';
    function selectImage2(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById('articleInageEcho2').src = evt.target.result;
            image = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
</script>




<table id="allArticle" class="easyui-datagrid" style="width:400px;height:250px">
    <input id="sss"  class="easyui-searchbox" searcher="searcher" prompt="请输入查询内容"  style="width:300px"></input>
    <a id="standardQueryBtn" href="javascript:void(0)" ONCLICK="doSelectLucene()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">高级搜索</a>
</table>


<!--添加的对话框表  -->
<div id="addArticleDialog" >
    <form id="addform" enctype="multipart/form-data" method="post">
        图片：<input type="file" value="" name="Image" onchange="selectImage(this);">
        <div class="lf salebd">
            <a href="#"><img id="articleInageEcho" src="images/ad20.jpg" width="100" height="100" /></a>
        </div><br/>
        文章名：<input  name="articleName"><br/>
        内容：<input  name="articleCount"><br/>
        上师姓名<select id="guruId" name="guruId"></select><br/><br/>
    </form>
</div>

<!--修改的对话框  -->
<div id="updateArticleDialog" enctype="multipart/form-data">
    <form id="upArticledateform" method="post"  enctype="multipart/form-data" class="easyui-form" data-options="novalidate:true" >
        <input id="articleId"  name="articleId" style="display: none">
        <input id="articleImage"  name="articleImage" style="display: none">
        封面：<input  type="file" class="easyui-filebox" name="albumImage" onchange="selectImage2(this);" data-options="required:true,missingMessage:'请选择封面'"/><br/>
        <div class="lf salebd">
            <a href="#"><img id="articleInageEcho2" src="" width="100" height="100" /></a>
        </div><br/>
        标题：<input id="articleName" name="articleName"><br/>
        内容：<input id="articleCount"  name="articleCount"><br/>
        状态：<br/>显示<input id="radio1" type="radio" value="0" name="articleState"><br/>
                    不显示<input id="radio2" type="radio" value="1" name="articleState"><br/>
    </form>
</div>
<!--修改的对话框----------------end  -->
